/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog.manualtraining;

import com.googlecode.jaden.ui.dialog.BaseDialog;
import static com.googlecode.jaden.ui.Constants.*;

import com.googlecode.jaden.ui.UiUtils;
import com.googlecode.jaden.ui.Constants;
import com.googlecode.jaden.common.other.JadenException;
import com.googlecode.jaden.common.other.Utils;
import com.googlecode.jaden.common.config.IOConfiguration;

import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.prefs.Preferences;

public class TestResultsDialog extends BaseDialog {

    public TestResultsDialog(Window owner, Preferences preferences,
                             IOConfiguration[] inputConfigurations, IOConfiguration outputConfiguration,
                             String[][] trainingInputs, String[] trainingOutputs, String[] trainingActuals,
                             String[][] validationInputs, String[] validationOutputs, String[] validationActuals,
                             String[][] testingInputs, String[] testingOutputs, String[] testingActuals) {
        super(owner, DIALOG_TEST_RESULTS_TITLE);


        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add(Utils.translate(LABEL_TRAINING_TEXT), buildScrollPane(inputConfigurations, outputConfiguration, trainingInputs, trainingOutputs, trainingActuals));
        tabbedPane.add(Utils.translate(LABEL_VALIDATION_TEXT), buildScrollPane(inputConfigurations, outputConfiguration, validationInputs, validationOutputs, validationActuals));
        tabbedPane.add(Utils.translate(LABEL_TESTING_TEXT), buildScrollPane(inputConfigurations, outputConfiguration, testingInputs, testingOutputs, testingActuals));

        setMainPanel(UiUtils.verticalList(UiUtils.horizontalList(tabbedPane)), preferences);
    }

    protected void doOk() throws JadenException {
        dispose();
    }

    private JScrollPane buildScrollPane(IOConfiguration[] inputConfigurations, IOConfiguration outputConfiguration,
                                        String[][] inputs, String[] outputs, String[] actuals) {
        Object[][] data = new Object[inputs.length][inputConfigurations.length + 2]; // ins + out + actual
        Object[] columnNames = new Object[inputConfigurations.length + 2];
        for (int col = 0; col < inputConfigurations.length + 2; col++) {
            if (col == inputConfigurations.length) {
                columnNames[col] = outputConfiguration.getName();
            } else if (col == inputConfigurations.length + 1) {
                columnNames[col] = Utils.translate(Constants.LABEL_ACTUAL_TEXT);
            } else {
                columnNames[col] = inputConfigurations[col].getName();
            }
            for (int row = 0; row < inputs.length; row++) {
                if (col == inputConfigurations.length) {
                    data[row][col] = outputs[row];
                } else if (col == inputConfigurations.length + 1) {
                    data[row][col] = actuals[row];
                } else {
                    data[row][col] = inputs[row][col];
                }
            }
        }

        TableModel tableModel = new DefaultTableModel(data, columnNames) {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        JTable trainingTable = new JTable(tableModel);
        return new JScrollPane(trainingTable);
    }
}
